package cn.fff.system.service.impl;

import cn.fff.model.system.SysMenu;
import cn.fff.model.vo.RouterVo;
import cn.fff.system.exception.FffCustomException;
import cn.fff.system.mapper.SysMenuMapper;
import cn.fff.system.service.SysMenuService;
import cn.fff.system.utils.RouterHelper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author apple
 * @description 针对表【sys_menu(菜单表)】的数据库操作Service实现
 * @createDate 2024-07-31 21:22:47
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Override
    public List<SysMenu> findNodes() {
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<SysMenu>()
                .orderByAsc(SysMenu::getSortValue);
        List<SysMenu> allMenus = this.list(wrapper);
        return buildTree(allMenus);
    }

    private List<SysMenu> buildTree(List<SysMenu> allMenus) {
        Map<String, SysMenu> menuMap = new HashMap<>();
        allMenus.forEach(e -> menuMap.put(e.getId(), e));
        List<SysMenu> result = new ArrayList<>();
        for (SysMenu menu : allMenus) {
            if (menu.getParentId().equals(0L)) {
                result.add(menu);
            } else {
                SysMenu parentMenu = menuMap.get(menu.getParentId().toString());
                if (parentMenu != null) {
                    if (parentMenu.getChildren() == null) {
                        parentMenu.setChildren(new ArrayList<>());
                    }
                    parentMenu.getChildren().add(menu);
                }
            }
        }
        return result;
    }

    @Override
    public void removeMenuById(String id) {
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<SysMenu>()
                .eq(SysMenu::getParentId, Long.valueOf(id));
        if (this.count(wrapper) > 0) {
            throw new FffCustomException(602, "请先删除子菜单");
        } else {
            this.removeById(id);
        }
    }

    @Override
    public List<RouterVo> getUserMenuListByUserId(String id) {

        List<SysMenu> sysMenus;
        if (Objects.equals(id, "1")) {
            sysMenus = this.list(new LambdaQueryWrapper<SysMenu>()
                    .eq(SysMenu::getStatus, 1)
                    .orderByAsc(SysMenu::getParentId)
                    .orderByAsc(SysMenu::getSortValue));
        } else {
            sysMenus = this.baseMapper.queryMenusByUserId(id);
        }
        // 构建成树形结构
        List<SysMenu> menuTree = buildTree(sysMenus);
        // 转换成前端要求的格式
        return RouterHelper.buildRouters(menuTree);
    }

    @Override
    public List<String> getUserButtonList(String id) {
        List<SysMenu> sysMenus;
        if ("1".equals(id)) {
            sysMenus = this.list(new LambdaQueryWrapper<SysMenu>()
                    .eq(SysMenu::getStatus, 1));
        } else {
            sysMenus = this.baseMapper.queryMenusByUserId(id);
        }
        List<String> permissionList = new ArrayList<>();
        sysMenus.forEach(e -> {
            if (e.getType() == 2) {
                permissionList.add(e.getPerms());
            }
        });
        return permissionList;
    }

//    private void sortMenu(List<SysMenu> menus) {
//        if (menus == null) return;
//        menus.sort(Comparator.comparing(SysMenu::getSortValue));
//        menus.forEach(e -> sortMenu(e.getChildren()));
//    }
}




